﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-tabs v-model="activeTab">
			<el-tab-pane label="基础信息" name="main">
				<el-card shadow="hover" class="layout-padding-auto create-form">
					<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>基本信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Name_FormItem" prop="Entity.Name" :rules="[{ required: true, message: '姓名为必填项', trigger: 'blur' }]">
										<template #label>
											<WtmLabelTooltip label="姓名" tooltip="员工姓名" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Name" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_StaffNo_FormItem"
										prop="Entity.StaffNo"
										:rules="[
											{ required: true, message: '工号为必填项', trigger: 'blur' },
											{ validator: validateStaffNo, trigger: 'blur' },
										]"
									>
										<template #label>
											<WtmLabelTooltip label="工号" tooltip="员工工号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.StaffNo" maxlength="10" show-word-limit clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Order_FormItem" prop="Entity.Order">
										<template #label>
											<WtmLabelTooltip label="排序" tooltip="排序" />
										</template>
										<el-input-number v-model="stateData.vmModel.Entity.Order" :min="0" clearable class="width100"></el-input-number>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_WorkYears_FormItem" prop="Entity.WorkYears">
										<template #label>
											<WtmLabelTooltip label="工龄" tooltip="工龄" />
										</template>
										<el-input-number
											v-model="stateData.vmModel.Entity.WorkYears"
											:min="0"
											:precision="1"
											clearable
											class="width100"
										></el-input-number>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_IdCardNo_FormItem"
										prop="Entity.IdCardNo"
										:rules="[{ required: true, message: '身份证号为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="身份证号" tooltip="身份证号码" />
										</template>
										<el-input
											v-model="stateData.vmModel.Entity.IdCardNo"
											@blur="parseIdCardInfo"
											clearable
											class="width100"
											disabled
											placeholder="请上传身份证正反面照片"
										></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_BirthDate_FormItem"
										prop="Entity.BirthDate"
										:rules="[{ required: true, message: '出生日期为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="出生日期" tooltip="出生日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.BirthDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Age_FormItem" prop="Entity.Age">
										<template #label>
											<WtmLabelTooltip label="年龄" tooltip="员工年龄" />
										</template>
										<el-input-number v-model="stateData.vmModel.Entity.Age" :min="0" clearable class="width100"></el-input-number>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_Gender_FormItem"
										prop="Entity.Gender"
										:rules="[{ required: true, message: '性别为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="性别" tooltip="性别" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.Gender" clearable class="width100">
											<el-option key="男" value="男" label="男"></el-option>
											<el-option key="女" value="女" label="女"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Ethnicity_FormItem" prop="Entity.Ethnicity">
										<template #label>
											<WtmLabelTooltip label="民族" tooltip="民族" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.Ethnicity" clearable class="width100">
											<el-option key="汉族" value="汉族" label="汉族"></el-option>
											<el-option key="回族" value="回族" label="回族"></el-option>
											<el-option key="满族" value="满族" label="满族"></el-option>
											<el-option key="蒙古族" value="蒙古族" label="蒙古族"></el-option>
											<el-option key="壮族" value="壮族" label="壮族"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_HighestEducation_FormItem" prop="Entity.HighestEducation">
										<template #label>
											<WtmLabelTooltip label="最高学历" tooltip="最高学历" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.HighestEducation" clearable class="width100">
											<el-option key="小学" value="小学" label="小学"></el-option>
											<el-option key="初中" value="初中" label="初中"></el-option>
											<el-option key="高中" value="高中" label="高中"></el-option>
											<el-option key="中专" value="中专" label="中专"></el-option>
											<el-option key="大专" value="大专" label="大专"></el-option>
											<el-option key="本科" value="本科" label="本科"></el-option>
											<el-option key="研究生" value="研究生" label="研究生"></el-option>
											<el-option key="硕士" value="硕士" label="硕士"></el-option>
											<el-option key="博士" value="博士" label="博士"></el-option>
											<el-option key="博士后" value="博士后" label="博士后"></el-option>
											<el-option key="博士研究生" value="博士研究生" label="博士研究生"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_MajorCategory_FormItem"
										prop="Entity.MajorCategory"
										:rules="[{ required: true, message: '专业大类为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="专业大类" tooltip="专业大类" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.MajorCategory" clearable class="width100">
											<el-option key="化学工业" value="化学工业" label="化学工业"></el-option>
											<el-option key="工学" value="工学" label="工学"></el-option>
											<el-option key="理学" value="理学" label="理学"></el-option>
											<el-option key="医学" value="医学" label="医学"></el-option>
											<el-option key="农学" value="农学" label="农学"></el-option>
											<el-option key="管理学" value="管理学" label="管理学"></el-option>
											<el-option key="经济学" value="经济学" label="经济学"></el-option>
											<el-option key="法学" value="法学" label="法学"></el-option>
											<el-option key="文学" value="文学" label="文学"></el-option>
											<el-option key="教育学" value="教育学" label="教育学"></el-option>
											<el-option key="艺术学" value="艺术学" label="艺术学"></el-option>
											<el-option key="历史学" value="历史学" label="历史学"></el-option>
											<el-option key="哲学" value="哲学" label="哲学"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_MajorName_FormItem"
										prop="Entity.MajorName"
										:rules="[{ required: true, message: '专业名称为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="专业名称" tooltip="专业名称" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.MajorName" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_MaritalStatus_FormItem" prop="Entity.MaritalStatus">
										<template #label>
											<WtmLabelTooltip label="婚姻状况" tooltip="婚姻状况" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.MaritalStatus" clearable class="width100">
											<el-option key="未婚" value="未婚" label="未婚"></el-option>
											<el-option key="已婚" value="已婚" label="已婚"></el-option>
											<el-option key="离异" value="离异" label="离异"></el-option>
											<el-option key="丧偶" value="丧偶" label="丧偶"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_FertilityStatus_FormItem"
										prop="Entity.FertilityStatus"
										:rules="[{ required: true, message: '生育状况为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="生育状况" tooltip="生育状况" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.FertilityStatus" clearable class="width100">
											<el-option key="未育" value="未育" label="未育"></el-option>
											<el-option key="一育" value="一育" label="一育"></el-option>
											<el-option key="二育" value="二育" label="二育"></el-option>
											<el-option key="三育" value="三育" label="三育"></el-option>
											<el-option key="四育及以上" value="四育及以上" label="四育及以上"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PhotoId_FormItem" prop="Entity.PhotoId">
										<template #label>
											<WtmLabelTooltip label="照片" tooltip="请上传个人近身照" />
										</template>
										<wtm-upload-image v-model="stateData.vmModel.Entity.PhotoId" clearable class="width100"></wtm-upload-image>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_IsVeteran_FormItem"
										prop="Entity.IsVeteran"
										:rules="[{ required: true, message: '请选择是否退伍军人', trigger: 'change' }]"
									>
										<template #label>
											<WtmLabelTooltip label="是否退伍军人" tooltip="是否为退伍军人" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.IsVeteran" clearable class="width100">
											<el-option key="是" :value="true" label="是"></el-option>
											<el-option key="否" :value="false" label="否"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col v-if="stateData.vmModel.Entity.IsVeteran === true" :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_VeteranCertificateId_FormItem"
										prop="Entity.VeteranCertificateId"
										:rules="[{ required: stateData.vmModel.Entity.IsVeteran === true, message: '请上传退伍证图片', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="退伍证图片" tooltip="退伍证图片" />
										</template>
										<wtm-upload-image v-model="stateData.vmModel.Entity.VeteranCertificateId" clearable class="width100"></wtm-upload-image>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>身份信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_IdCardFrontPhotoId_FormItem"
										prop="Entity.IdCardFrontPhotoId"
										:rules="[{ required: true, message: '身份证人像面为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="身份证人像面" tooltip="身份证人像面是带头像的一面" />
										</template>
										<wtm-upload-image
											v-model="stateData.vmModel.Entity.IdCardFrontPhotoId"
											clearable
											class="width100"
											@update:model-value="handleUploadRefresh($event, 'front')"
										></wtm-upload-image>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_IdCardBackPhotoId_FormItem"
										prop="Entity.IdCardBackPhotoId"
										:rules="[{ required: true, message: '身份证国徽面为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="身份证国徽面" tooltip="身份证国徽面是带国徽的一面" />
										</template>
										<wtm-upload-image
											v-model="stateData.vmModel.Entity.IdCardBackPhotoId"
											clearable
											class="width100"
											@update:model-value="handleUploadRefresh($event, 'back')"
										></wtm-upload-image>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_IdCardExpiryDate_FormItem" prop="Entity.IdCardExpiryDate">
										<template #label>
											<WtmLabelTooltip label="身份证有效期" tooltip="身份证有效期" />
										</template>
										<el-input
											:value="other.formatIdCardExpiry(stateData.vmModel.Entity.IdCardExpiryDate)"
											clearable
											class="width100"
											disabled
											placeholder="上传身份证国徽面后自动填充"
										></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>户籍信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_HouseholdRegistration_FormItem" prop="Entity.HouseholdRegistration">
										<template #label>
											<WtmLabelTooltip label="户口所在地" tooltip="户口所在地" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.HouseholdRegistration" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_NativePlace_FormItem" prop="Entity.NativePlace">
										<template #label>
											<WtmLabelTooltip label="籍贯" tooltip="籍贯" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.NativePlace" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>政治面貌</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_PoliticalStatus_FormItem"
										prop="Entity.PoliticalStatus"
										:rules="[{ required: true, message: '政治面貌为必填项', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="政治面貌" tooltip="政治面貌" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.PoliticalStatus" clearable class="width100">
											<el-option key="中共党员" value="中共党员" label="中共党员"></el-option>
											<el-option key="共青团员" value="共青团员" label="共青团员"></el-option>
											<el-option key="群众" value="群众" label="群众"></el-option>
											<el-option key="无党派人士" value="无党派人士" label="无党派人士"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PartyJoinDate_FormItem" prop="Entity.PartyJoinDate">
										<template #label>
											<WtmLabelTooltip label="入党(团)日期" tooltip="入党或入团日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.PartyJoinDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_PartyBranch_FormItem" prop="Entity.PartyBranch">
										<template #label>
											<WtmLabelTooltip label="入党所在支部" tooltip="入党所在支部" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.PartyBranch" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveLocation_FormItem" prop="Entity.ArchiveLocation">
										<template #label>
											<WtmLabelTooltip label="档案所在地" tooltip="档案存放地" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.ArchiveLocation" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveNo_FormItem" prop="Entity.ArchiveNo">
										<template #label>
											<WtmLabelTooltip label="存档号" tooltip="档案编号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.ArchiveNo" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveAgeDate_FormItem" prop="Entity.ArchiveAgeDate">
										<template #label>
											<WtmLabelTooltip label="档案年龄日期" tooltip="档案年龄日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ArchiveAgeDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveTransferOutDate_FormItem" prop="Entity.ArchiveTransferOutDate">
										<template #label>
											<WtmLabelTooltip label="档案调出日期" tooltip="档案调出日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ArchiveTransferOutDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ArchiveTransferInDate_FormItem" prop="Entity.ArchiveTransferInDate">
										<template #label>
											<WtmLabelTooltip label="档案调入日期" tooltip="档案调入日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ArchiveTransferInDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>联系方式</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_HomeAddress_FormItem" prop="Entity.HomeAddress">
										<template #label>
											<WtmLabelTooltip label="家庭住址" tooltip="家庭住址" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.HomeAddress" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_Phone_FormItem"
										prop="Entity.Phone"
										:rules="[
											{ required: true, message: '手机号为必填项', trigger: 'blur' },
											{ validator: validatePhone, trigger: 'blur' },
										]"
									>
										<template #label>
											<WtmLabelTooltip label="手机号" tooltip="手机号码" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Phone" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Email_FormItem" prop="Entity.Email">
										<template #label>
											<WtmLabelTooltip label="邮箱" tooltip="电子邮箱" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Email" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>银行账户</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_SalaryBank_FormItem" prop="Entity.SalaryBank">
										<template #label>
											<WtmLabelTooltip label="工资卡银行" tooltip="工资卡开户银行" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.SalaryBank" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_SalaryAccount_FormItem" prop="Entity.SalaryAccount">
										<template #label>
											<WtmLabelTooltip label="工资卡号" tooltip="工资卡账号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.SalaryAccount" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BonusBank_FormItem" prop="Entity.BonusBank">
										<template #label>
											<WtmLabelTooltip label="奖金卡银行" tooltip="奖金卡开户银行" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.BonusBank" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BonusAccount_FormItem" prop="Entity.BonusAccount">
										<template #label>
											<WtmLabelTooltip label="奖金卡号" tooltip="奖金卡账号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.BonusAccount" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>工作信息</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_OrgId_FormItem" prop="Entity.OrgId">
										<template #label>
											<WtmLabelTooltip label="所属公司" tooltip="关联的公司" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.OrgId" clearable filterable class="width100">
											<el-option v-for="item in stateData.AllSysOrgs" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
										</el-select>
									</el-form-item>
								</el-col>

								<el-col :xs="24" :lg="12" class="mb20">
									<WtmDeptSelector v-model:model-value="stateData.vmModel.Entity.GroupId" prop="Entity.GroupId" label="所属部门" :auth="false" />
								</el-col>

								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item
										ref="Entity_JobPositionId_FormItem"
										prop="Entity.JobPositionId"
										:rules="[{ required: true, message: '请选择工作岗位', trigger: 'blur' }]"
									>
										<template #label>
											<WtmLabelTooltip label="工作岗位" tooltip="关联的工作岗位" />
										</template>
										<el-select
											v-model="stateData.vmModel.Entity.JobPositionId"
											clearable
											filterable
											class="width100"
											:disabled="!stateData.vmModel.Entity.GroupId"
										>
											<el-option v-for="item in stateData.AllSysJobPositions" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_JoinWorkDate_FormItem" prop="Entity.JoinWorkDate">
										<template #label>
											<WtmLabelTooltip label="参加工作日期" tooltip="首次参加工作日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.JoinWorkDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<!-- <el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ResumeId_FormItem" prop="Entity.ResumeId">
										<template #label>
											<WtmLabelTooltip label="简历" tooltip="员工简历文件" />
										</template>
										<wtm-upload-file v-model="stateData.vmModel.Entity.ResumeId" clearable class="width100"></wtm-upload-file>
									</el-form-item>
								</el-col> -->
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_HireDate_FormItem" prop="Entity.HireDate">
										<template #label>
											<WtmLabelTooltip label="入职日期" tooltip="本公司入职时间" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.HireDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ConfirmationDate_FormItem" prop="Entity.ConfirmationDate">
										<template #label>
											<WtmLabelTooltip label="转正日期" tooltip="转正日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ConfirmationDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_CompanyYears_FormItem" prop="Entity.CompanyYears">
										<template #label>
											<WtmLabelTooltip label="司龄" tooltip="司龄" />
										</template>
										<el-input-number
											v-model="stateData.vmModel.Entity.CompanyYears"
											:min="0"
											:precision="1"
											clearable
											class="width100"
											disabled
										></el-input-number>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_EmploymentType_FormItem" prop="Entity.EmploymentType">
										<template #label>
											<WtmLabelTooltip label="用工形式" tooltip="用工形式" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.EmploymentType" clearable class="width100">
											<el-option key="全职" value="全职" label="全职"></el-option>
											<el-option key="兼职" value="兼职" label="兼职"></el-option>
											<el-option key="实习" value="实习" label="实习"></el-option>
											<el-option key="外包" value="外包" label="外包"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractType_FormItem" prop="Entity.ContractType">
										<template #label>
											<WtmLabelTooltip label="劳动合同类型" tooltip="劳动合同类型" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.ContractType" clearable class="width100">
											<el-option key="固定期限" value="固定期限" label="固定期限"></el-option>
											<el-option key="无固定期限" value="无固定期限" label="无固定期限"></el-option>
											<el-option key="项目合同" value="项目合同" label="项目合同"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractNo_FormItem" prop="Entity.ContractNo">
										<template #label>
											<WtmLabelTooltip label="合同编号" tooltip="合同编号" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.ContractNo" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractStartDate_FormItem" prop="Entity.ContractStartDate">
										<template #label>
											<WtmLabelTooltip label="合同起始日" tooltip="劳动合同起始日" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ContractStartDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ContractEndDate_FormItem" prop="Entity.ContractEndDate">
										<template #label>
											<WtmLabelTooltip label="合同终止日" tooltip="劳动合同终止日" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ContractEndDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ResignationDate_FormItem" prop="Entity.ResignationDate">
										<template #label>
											<WtmLabelTooltip label="离职日期" tooltip="离职日期" />
										</template>
										<el-date-picker v-model="stateData.vmModel.Entity.ResignationDate" clearable class="width100"></el-date-picker>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ResignationReason_FormItem" prop="Entity.ResignationReason">
										<template #label>
											<WtmLabelTooltip label="离职原因" tooltip="离职原因" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.ResignationReason" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_ResignationType_FormItem" prop="Entity.ResignationType">
										<template #label>
											<WtmLabelTooltip label="离职类型" tooltip="离职类型" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.ResignationType" clearable class="width100">
											<el-option key="主动离职" value="主动离职" label="主动离职"></el-option>
											<el-option key="被动离职" value="被动离职" label="被动离职"></el-option>
											<el-option key="退休" value="退休" label="退休"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_State_FormItem" prop="Entity.State">
										<template #label>
											<WtmLabelTooltip label="人员状态" tooltip="人员状态" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.State" clearable class="width100">
											<el-option key="在职" value="在职" label="在职"></el-option>
											<el-option key="试用期" value="试用期" label="试用期"></el-option>
											<el-option key="离职" value="离职" label="离职"></el-option>
											<el-option key="退休" value="退休" label="退休"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
							</el-row>
						</div>

						<div class="form-section">
							<div class="section-title">
								<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
								<span>其他</span>
							</div>

							<el-row :gutter="20">
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_BloodType_FormItem" prop="Entity.BloodType">
										<template #label>
											<WtmLabelTooltip label="血型" tooltip="血型" />
										</template>
										<el-select v-model="stateData.vmModel.Entity.BloodType" clearable class="width100">
											<el-option key="A" value="A" label="A"></el-option>
											<el-option key="B" value="B" label="B"></el-option>
											<el-option key="AB" value="AB" label="AB"></el-option>
											<el-option key="O" value="O" label="O"></el-option>
											<el-option key="其他" value="其他" label="其他"></el-option>
										</el-select>
									</el-form-item>
								</el-col>
								<el-col :xs="24" :lg="12" class="mb20">
									<el-form-item ref="Entity_Hobbies_FormItem" prop="Entity.Hobbies">
										<template #label>
											<WtmLabelTooltip label="兴趣爱好特长" tooltip="兴趣爱好及特长" />
										</template>
										<el-input v-model="stateData.vmModel.Entity.Hobbies" clearable class="width100"></el-input>
									</el-form-item>
								</el-col>
							</el-row>
						</div>
					</el-form>
				</el-card>
			</el-tab-pane>
			<el-tab-pane label="学习经历" name="personeducations" v-if="stateData.vmModel.Entity.ID">
				<SysPersonEducation :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="家庭成员" name="personfamilies" v-if="stateData.vmModel.Entity.ID">
				<SysPersonFamily :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="工作经历" name="personworks" v-if="stateData.vmModel.Entity.ID">
				<SysPersonWork :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="培训考试" name="persontrainexams" v-if="stateData.vmModel.Entity.ID">
				<SysPersonTrainExam :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工体检" name="personmedicals" v-if="stateData.vmModel.Entity.ID">
				<SysPersonMedical :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工履历" name="personresumes" v-if="stateData.vmModel.Entity.ID">
				<SysPersonResume :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="员工合同" name="personcontracts" v-if="stateData.vmModel.Entity.ID">
				<SysPersonContract :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>

			<el-tab-pane label="荣誉职称" name="personprofessionaltitles" v-if="stateData.vmModel.Entity.ID">
				<SysPersonProfessionalTitle :search-bar="false" :toolbar="true" :is-disabled="true" :person-id="stateData.vmModel.Entity.ID" />
			</el-tab-pane>
		</el-tabs>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, nextTick, type ComponentInternalInstance, watch } from 'vue';
import { SysPersonApi as api } from '/@/views/person/sysperson/api';
import other from '/@/utils/other';
import { useIdCardParser } from '/@/hooks/useCardParser';
import { useIdCardOcr } from '/@/hooks/useIdCardOcr';
import { ISysPersonVm } from '/@/views/person/sysperson/api/interface';
import { IViewModel } from '/@/types/common';

import SysPersonEducation from '/@/views/person/syspersoneducation/index.vue';
import SysPersonFamily from '/@/views/person/syspersonfamily/index.vue';
import SysPersonWork from '/@/views/person/syspersonwork/index.vue';
import SysPersonTrainExam from '/@/views/person/syspersontrainexam/index.vue';
import SysPersonMedical from '/@/views/person/syspersonmedical/index.vue';
import SysPersonResume from '/@/views/person/syspersonresume/index.vue';
import SysPersonContract from '/@/views/person/syspersoncontract/index.vue';
import SysPersonProfessionalTitle from '/@/views/person/syspersonprofessionaltitle/index.vue';

// 常量定义
const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	CANCEL: '取消',
} as const;

const MESSAGE_TEXTS = {
	SAVE_SUCCESS: '保存成功',
} as const;

const API_ENDPOINTS = {
	ORGS: '/api/SysPerson/GetSysOrgs',
	GROUPS: '/api/SysPerson/GetSysGroups',
	JOB_POSITIONS: '/api/SysPerson/GetSysJobPositions',
} as const;

const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const activeTab = ref('main');
const hasSavedOnce = ref(false);
const workYearsDisplay = ref(''); // 工龄显示值

// 工号数字验证
const validateStaffNo = (rule: any, value: string, callback: any) => {
	if (!value) {
		callback(); // 如果值为空，让 required 规则处理
		return;
	}

	if (!/^\d+$/.test(value)) {
		callback(new Error('工号只能包含数字'));
		return;
	}

	callback();
};

// 使用通用的手机号验证方法
const validatePhone = other.validatePhone;

// 定义变量内容
const stateData = reactive<IViewModel<ISysPersonVm>>({
	vmModel: {
		Entity: {
			Name: null,
			StaffNo: null,
			BirthDate: null,
			Age: null,
			Gender: null,
			Ethnicity: null,
			HighestEducation: null,
			MajorCategory: null,
			MajorName: null,
			MaritalStatus: null,
			FertilityStatus: null,
			PhotoId: null,
			IdCardNo: null,
			IdCardFrontPhotoId: null,
			IdCardBackPhotoId: null,
			HouseholdRegistration: null,
			NativePlace: null,
			PoliticalStatus: null,
			PartyJoinDate: null,
			PartyBranch: null,
			ArchiveLocation: null,
			ArchiveNo: null,
			ArchiveTransferOutDate: null,
			ArchiveTransferInDate: null,
			HomeAddress: null,
			Phone: null,
			Email: null,
			SalaryBank: null,
			SalaryAccount: null,
			BonusBank: null,
			BonusAccount: null,
			OrgId: null,
			GroupId: null,
			JobPositionId: null,
			ResumeId: null,
			HireDate: null,
			ConfirmationDate: null,
			EmploymentType: null,
			ContractType: null,
			ContractNo: null,
			ContractStartDate: null,
			ContractEndDate: null,
			ResignationDate: null,
			ResignationReason: null,
			ResignationType: null,
			State: '在职',
			BloodType: null,
			Hobbies: null,
			IdCardExpiryDate: null,
			JoinWorkDate: null,
			ArchiveAgeDate: null,
			IsVeteran: null,
			VeteranCertificateId: null,
			Order: null,
			WorkYears: null,
			CompanyYears: null,
		},
	},

	AllSysOrgs: [] as any[],
	AllSysGroups: [] as any[],
	AllSysJobPositions: [] as any[],
});

// 初始化身份证OCR hooks
const { recognizeIdCard } = useIdCardOcr({
	autoFill: true,
	showMessage: true,
});

// 身份证上传识别处理
const handleUploadRefresh = async (value: string, type: 'front' | 'back') => {
	if (!value) return;
	await recognizeIdCard(value, type, stateData.vmModel.Entity);
};

// 解析身份证号码信息
const parseIdCardInfo = () => {
	if (stateData.vmModel.Entity.IdCardNo) {
		useIdCardParser().parseAndFill(stateData.vmModel.Entity.IdCardNo, stateData.vmModel.Entity);
	}
};

// 统一的成功处理函数
const handleSuccess = (response: any) => {
	ElMessage.success(MESSAGE_TEXTS.SAVE_SUCCESS);
	emit('refresh');

	// 获取返回的ID并赋值，以便显示其他tabs
	if (response && response.Entity && response.Entity.ID) {
		stateData.vmModel.Entity = response.Entity;
	} else if (response && response.ID) {
		(stateData.vmModel.Entity as any).ID = response.ID;
	}

	hasSavedOnce.value = true;
	// 不关闭弹窗，让用户可以继续编辑其他tabs
	loading.value = false;

	nextTick(() => {
		setupHeaderButtons();
	});
};

// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 公共的API请求处理函数
const handleApiRequest = (apiCall: () => Promise<any>) => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			apiCall().then(handleSuccess).catch(handleError);
		}
	});
};

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	handleApiRequest(() => {
		// 创建一个深拷贝，确保新增时不发送无效的 ID 字段
		const submitData = JSON.parse(JSON.stringify(stateData.vmModel));
		if (!submitData.Entity.ID) {
			// 如果没有有效的 ID，删除该字段
			delete submitData.Entity.ID;
		}
		return api().add(submitData);
	});
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		(stateData.vmModel.Entity as any).ID = wtmdata.ID;
		hasSavedOnce.value = true;
	}
};
// 获取岗位数据的方法
const getSysJobPositions = (groupId?: string) => {
	const url = groupId ? `/api/SysUserJobPosition/GetSysJobPositions?groupId=${groupId}` : '/api/SysUserJobPosition/GetSysJobPositions';
	return other.getSelectList(url, [], false).then((x) => {
		stateData.AllSysJobPositions = x;
	});
};
// 监听部门选择变化
watch(
	() => stateData.vmModel.Entity.GroupId,
	(newGroupId) => {
		if (newGroupId) {
			// 清空当前选择的岗位
			stateData.vmModel.Entity.JobPositionId = null;
			// 根据选择的部门获取对应的岗位
			getSysJobPositions(newGroupId);
		} else {
			// 如果没有选择部门，清空岗位数据
			stateData.AllSysJobPositions = [];
			stateData.vmModel.Entity.JobPositionId = null;
		}
	}
);

// 计算工龄
watch(
	() => stateData.vmModel.Entity.JoinWorkDate,
	(newJoinWorkDate) => {
		if (newJoinWorkDate) {
			stateData.vmModel.Entity.WorkYears = other.calculateWorkYears(newJoinWorkDate).decimalYears;
		}
	}
);

// 计算司龄
watch(
	() => stateData.vmModel.Entity.HireDate,
	(newHireDate) => {
		if (newHireDate) {
			const companyYearsResult = other.calculateWorkYears(newHireDate);
			stateData.vmModel.Entity.CompanyYears = companyYearsResult.decimalYears;
		} else {
			stateData.vmModel.Entity.CompanyYears = null;
		}
	}
);
// 加载页面数据
const loadPageData = async () => {
	const [orgs] = await Promise.allSettled([other.getSelectList(API_ENDPOINTS.ORGS, [], false)]);

	// 处理成功的结果
	if (orgs.status === 'fulfilled') stateData.AllSysOrgs = orgs.value;
};

// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		await loadPageData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.SAVE,
			type: 'primary',
			onClick: onSubmit,
			visible: !hasSavedOnce.value, // 未保存过时显示保存按钮
		},
		{
			text: BUTTON_TEXTS.CANCEL,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
